package cn.com.anypay.manager.miaoma.manual.providerchannelconfig;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.anypay.manager.miaoma.manual.providerchannelconfig.mapper.ProviderChannelConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 服务商渠道配置服务实现类
 */
@Service
public class ProviderChannelConfigServiceImpl extends ServiceImpl<ProviderChannelConfigMapper, ProviderChannelConfig> implements ProviderChannelConfigService {

    @Override
    public IPage<ProviderChannelConfig> queryByPage(Page<ProviderChannelConfig> page, Long providerId, Long channelId, Integer status) {
        LambdaQueryWrapper<ProviderChannelConfig> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (providerId != null) {
            wrapper.eq(ProviderChannelConfig::getProviderId, providerId);
        }
        if (channelId != null) {
            wrapper.eq(ProviderChannelConfig::getChannelId, channelId);
        }
        if (status != null) {
            wrapper.eq(ProviderChannelConfig::getStatus, status);
        }

        // 按更新时间倒序排列
        wrapper.orderByDesc(ProviderChannelConfig::getUpdatedAt);

        return this.page(page, wrapper);
    }

    @Override
    public boolean saveOrUpdateConfig(ProviderChannelConfig config) {
        if (config.getId() == null) {
            // 新增
            config.setCreatedAt(LocalDateTime.now());
            config.setUpdatedAt(LocalDateTime.now());
            return this.save(config);
        } else {
            // 更新
            config.setUpdatedAt(LocalDateTime.now());
            return this.updateById(config);
        }
    }
}